<?php
/**
 * Plugin Name: BSC Widget Custom Post Show for Space Design
 * Plugin URI: http://www.bridgesytem.me
 * Description: BSC Widget Custom Post Show for Space Design
 * Version: 1.0
 * Author: Tran Cuong Truc
 * Author URI: http://www.trancuongtruc.net
 * License: Private GPL2
 */
class widget_custom_post_show extends WP_Widget {
    function widget_custom_post_show() {
       // parent::WP_Widget(false, $name='Custom Post Show Widget');
    
       
    $widget_ops = array('classname' => 'widget_custom_post_show', 'description' => __( "Widget Custom Post Show Options", 'widget_custom_post_show') );
    $this->WP_Widget('widget_custom_post_show', __('Custom Post Show', 'widget_custom_post_show'), $widget_ops);

    }
    
    function widget($args,$instance) {
        extract($args);
        $title = apply_filters('widget_title',$instance['title']);
        ?>
            <?php echo $before_widget; ?>
            <h2 class="sp_col_title">
            <?php if($title) {
                echo $title; 
            } ?>
            </h2>
             <?php wp_reset_query(); ?>
            <?php query_posts("posts_per_page={$instance["number_of_post"]}&cat={$instance["cat_id"]}") ?>
             <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
                          <div class="sp_col_content">
                              <p>Posted: <?php the_time('d/m/Y'); ?> at <?php the_time('g:i a'); ?></p>
                          <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                              <?php the_content(); ?>
                          </div>
                      <?php endwhile;
                  endif; ?>
                   <?php wp_reset_postdata();?>
            <?php echo $after_widget; ?>
        <?php
    }
    
    function update($new_instance,$old_instance) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['number_of_post'] = strip_tags($new_instance['number_of_post']);
        $instance['cat_id'] = strip_tags($new_instance['cat_id']);
        return $instance;
    }
    
    function form($instance) {
        global $wpdb;
        $title = esc_attr($instance['title']);
        $number_of_post = esc_attr($instance['number_of_post']);
        $category_id = esc_attr($instance['cat_id']);
        ?>
            <p>
                <label for="<?php echo $this->get_field_id('title'); ?>">
                    <?php echo "Title: ";?>
                </label>
                <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>"
                       name="<?php echo $this->get_field_name('title'); ?>"
                       type="text" value="<?php echo $title; ?>" />
            </p>
             <p>
                <label for="<?php echo $this->get_field_id('number_of_post'); ?>">
                    <?php echo "Number of posts: ";?>
                </label>
                <input class="widefat" id="<?php echo $this->get_field_id('number_of_post'); ?>"
                       name="<?php echo $this->get_field_name('number_of_post'); ?>"
                       type="text" value="<?php echo $number_of_post; ?>" />
            </p>
             <p>
                <label for="<?php echo $this->get_field_id('cat_id'); ?>">
                    <?php echo "Category Name: ";?>
                </label>
                <select class="widefat" id="<?php echo $this->get_field_id('cat_id'); ?>"
                        name="<?php echo $this->get_field_name('cat_id'); ?>">
                    <?php $results = $wpdb->get_results("SELECT * FROM wp_terms
                                                            WHERE wp_terms.term_id IN 
                                                            (SELECT term_id FROM wp_term_taxonomy 
                                                            WHERE wp_term_taxonomy.taxonomy = 'category')");
                    foreach($results as $result) {
                    ?>
                    <option value="<?php echo $result->term_id; ?>" <?php echo ($category_id == $result->term_id) ? "selected='selected'" : "" ?>><?php echo $result->name; ?></option>
                    <?php } ?>
                </select>
            </p>
        <?php
    }
    
    
}

function wcps_init() {
    register_widget('widget_custom_post_show');
}

add_action('widgets_init','wcps_init');

